// pages/sells/sellsList/sellsList.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnStyle: ['#bf9368', '#3a3938', '#3a3938'],
    pageNum: '1', //分页
    resultData: [], //数据源
    imgURL: app.globalData.imgURL, //图片总路径
    url: 'buyerpurinfo/queryAllOrdersList',
    orderSData: [],
    showViewId: 2,
    noOrdersMsg: '',
    selectID: 1,
    userInfo: {},
    orderStatus: "0",
    openId: '',
    isLogin: 0,
  },
  toRelease: function (e) {
    wx.navigateTo({
      url: '../releaseInfo/releaseInfo',
    })

  },
  showOrderDetail: function (e) {
    var that = this;
    wx.navigateTo({
      url: '../../buys/buysDetail/buysDetail?id=' + e.currentTarget.id + "&openId=" + that.data.openId + "&type=" + that.data.customerType,
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var that = this;
    // if (wx.getStorageSync('userInfo')){
    //   that.setData({
    //     userInfo: wx.getStorageSync('userInfo'),//获取用户信息
    //     openId: wx.getStorageSync('userInfo').userId //获取openId
    //   })
    // }

    // console.log('that.data.userInfo');

    // console.log(that.data.userInfo);
  },

  //请求数据
  requestData: function () {
    var that = this;

    app.post(that.data.url, {
      pageNum: that.data.pageNum,
      orderStatus: that.data.orderStatus
    }).then((res) => {
      console.log('success');
      console.log(res);
      if (res.code == 0) {
        that.setData({
          orderSData: that.data.orderSData.concat(res.resultData),
        })

      } else {
        wx.showModal({
          title: '温馨提示',
          content: res.msg,
          showCancel: false,
        })
      }
      console.log("订单数量：" + this.data.orderSData.length);
      if (that.data.orderSData.length == 0) {
        that.setData({
          showViewId: 2,
          noOrdersMsg: '未查询到订单信息~~',
        })
      } else {
        that.setData({
          showViewId: 1,
        })
      }
    }).catch((errMsg) => {
      console.log('fail');
      console.log(errMsg + '失败');
    })
  },
  typeChoose: function (e) {
    var that = this;
    console.log('切换 未完成订单，已完成订单');

    if (e.currentTarget.id == that.data.selectID) {
      return;
    }
    console.log(e.currentTarget.id)
    if (e.currentTarget.id == 1) {
      //未完成订单
      that.setData({
        btnStyle: ['#bf9368', '#3a3938', '#3a3938'],
        selectID: e.currentTarget.id,
        pageNum: '1', //分页
        url: 'buyerpurinfo/queryAllOrdersList', //我要卖未完成订单
        orderSData: [], //列表数据
        orderStatus: "0",
      })
    } else if (e.currentTarget.id == 2) {
      //优惠促销
      that.setData({
        btnStyle: ['#3a3938', '#bf9368', '#3a3938'],
        selectID: e.currentTarget.id,
        pageNum: '1', //分页
        url: 'buyerpurinfo/queryAllOrdersList', //我要卖已完成订单
        listData: [], //列表数据
        orderSData: [], //未完成订单列表
        orderStatus: '1',
      })
    }
    that.requestData();
  },
  res_goods_info: function (e) {
    // wx.navigateTo({
    //   url: '../sellsReleaseInfo/sellsReleaseInfo?buyId=' + e.currentTarget.id,
    // })
    var that = this;
    wx.navigateTo({
      url: '../../buys/buysDetail/buysDetail?id=' + e.currentTarget.id + "&openId=" + that.data.openId + "&type=" + that.data.customerType + "&showReleaseBtn=show&toReleaseType=seller",
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (wx.getStorageSync('userInfo')) {
      that.setData({
        userInfo: wx.getStorageSync('userInfo'), //获取用户信息
        openId: wx.getStorageSync('userInfo').userId, //获取openId
        orderSData: [],
        pageNum: 1,
      });
      app.post("customer/queryUserRoleByOpenId", {
        openId: that.data.openId,
        customerType: "1"
      }).then((res) => {
        console.log(res);
        if (res.code == 0) {
          if (res.resultData.status == "1") {
            that.setData({
              isLogin: 1,
            });
            that.requestData();
          } else {
            wx.showModal({
              title: '温馨提示',
              content: "审核中，审核通过后才可查看",
              showCancel: false,
              success(res) {
                if (res.confirm) {
                  wx.reLaunch({
                    url: '../../user/userFList/userFList',
                  })
                }
              }
            })
          }

        } else if (res.code == 1) {
          wx.showModal({
            title: '温馨提示',
            content: "对不起，您没有查看该页面权限",
            showCancel: false,
            success(res) {
              if (res.confirm) {
                wx.reLaunch({
                  url: '../../user/userFList/userFList',
                })
              }
            }
          })
        } else {
          wx.showModal({
            title: '温馨提示',
            content: res.msg,
            showCancel: false,
          })
        }
      }).catch((errMsg) => {
        console.log('fail');
        console.log(errMsg + '失败');
      })
    } else {
      that.setData({
        isLogin: 0,
      })
      wx.showModal({
        title: '提示',
        content: '请先登录！！',
        showCancel: false,
        success(res) {
          if (res.confirm) {
            wx.reLaunch({
              url: '../../user/userFList/userFList',
            })
          }
        }
      })


    }

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      orderSData: [],
      pageNum: '1', //分页
      userInfo: wx.getStorageSync('userInfo'), //获取用户信息
    })
    wx.showNavigationBarLoading();
    setTimeout(function () {
      that.requestData();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    }, 2000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('加载更多');
    var that = this;
    that.data.pageNum++;
    console.log(that.data.pageNum);
    that.requestData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})